<template>
  <div class="register-container">
    <el-card>
      <h2>用户注册</h2>
      <el-form :model="form" ref="form" @submit.prevent="handleRegister">
        <el-form-item label="用户名" prop="username" :rules="[{ required: true, message: '请输入用户名', trigger: 'blur' }]">
          <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        
        <el-form-item label="电子邮件" prop="email" :rules="[{ required: true, message: '请输入电子邮件', trigger: 'blur' }, { type: 'email', message: '请输入有效的电子邮件地址' }]">
          <el-input v-model="form.email" placeholder="请输入电子邮件"></el-input>
        </el-form-item>

        <el-form-item label="手机号码" prop="phone" :rules="[{ required: true, message: '请输入手机号码', trigger: 'blur' }]">
          <el-input v-model="form.phone" placeholder="请输入手机号码"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password" :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]">
          <el-input v-model="form.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword" :rules="[{ required: true, message: '请确认密码', trigger: 'blur' }]">
          <el-input v-model="form.confirmPassword" type="password" placeholder="请确认密码"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleRegister">注册</el-button>
          <el-button @click="goToLogin">返回登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Register',
  data() {
    return {
      form: {
        username: '',
        email: '',
        phone: '',
        password: '',
        confirmPassword: ''
      }
    };
  },
  methods: {
    async handleRegister() {
      // 这里可以添加注册逻辑，例如调用 API
      if (this.form.password !== this.form.confirmPassword) {
        this.$message.error('两次输入的密码不一致');
        return;
      }
      // 假设注册成功
      this.$message.success('注册成功！');
      this.$router.push('/login'); // 注册成功后跳转到登录页面
    },
    goToLogin() {
      this.$router.push('/login'); // 返回登录页面
    }
  }
}
</script>

<style scoped>
.register-container {
  max-width: 400px;
  margin: 100px auto;
}

.el-form-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-form-item label {
  width: 100px; /* 设置标签宽度 */
  text-align: right; /* 标签右对齐 */
  margin-right: 10px; /* 标签与输入框之间的间距 */
}
</style> 